{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2.35446051]\n",
      " [ 3.83540597]] 4.30940925625 1.38271355762\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAYcAAAD8CAYAAACcjGjIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAGqFJREFUeJzt3X+M3PV95/Hna2bWa4J/Agsxtokd6qRxuIshe8S6NBWFFmx0qcldcjK6K1YOyb0e6BKldxen0R25/JDKVQkSEqFyDismSmMoSYrVOudYHNe0agEbMGDjUG8cGi92bIMBmxJs7/p9f3w/Y4935juzO/bOjPV9PaTRzrzn+515z3fH+/L38/l+ZxQRmJmZ1Sp1uwEzM+s9DgczM6vjcDAzszoOBzMzq+NwMDOzOg4HMzOr43AwM7M6DgczM6vjcDAzszqVbjfQrksuuSQWLFjQ7TbMzM4rTz/99KsRMdBqufM2HBYsWMC2bdu63YaZ2XlF0j+OZ7mWw0qSpkp6StJzknZK+p+pvlDSk5J2S3pI0pRU70+3h9L9C2oe6wup/pKkm2rqy1JtSNKaib5YMzM7t8Yz53AMuD4iPgQsAZZJWgrcDdwTEYuA14Hb0/K3A69HxK8B96TlkLQYWAl8EFgGfFNSWVIZuA9YDiwGbk3LmplZl7QMh8i8lW72pUsA1wOPpPp64JZ0fUW6Tbr/BklK9Q0RcSwifg4MAdemy1BE7ImI48CGtKyZmXXJuI5WSv/D3w4cBLYAPwPeiIiRtMgwMDddnwvsBUj3vwlcXFsfs05evVEfqyVtk7Tt0KFD42ndzMzaMK5wiIjRiFgCzCP7n/4HGi2WfirnvonWG/WxNiIGI2JwYKDlZLuZmbVpQuc5RMQbwP8DlgKzJFWP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XeiraiVn/oPt9vaqyttGvfTe+w9k/8OsWijpWUl/LeljXein0e+uV7bXx4ADEbG7ptbx7TXm70PH3mNFCodG38jd9UO1JE0Dvg98NiKOAPcDVwJLgP1ku7Wd9tGIuAZYDtwh6Te70ENDyr5G9neBP0+lXtherfTEe0/SF4ER4LuptB+4IiKuBj4H/JmkGR1sKe931xPbC7iVM/8T0vHt1eDvQ+6iDWpntc2KFA7DwPya2/OAfV3qBQBJfWS/+O9GxA8AIuJARIxGxEngW0zS7nQzEbEv/TwI/DD1cKC6m5p+Hux0X8ly4JmIOJB67Pr2qpG3jbr+3pO0CvhXwL+LNEidhm1eS9efJhvbf1+nemryu+uF7VUB/jXwULXW6e3V6O8DHXyPFSkctgKLJC1M//tcCWzsVjNpPPMBYFdEfKOmXjtO+Algx9h1J7mvCyVNr14nm8zcQbatVqXFVgGPdrKvGmf8b67b22uMvG20EbgtHVGyFHizOjTQCZKWAZ8Hfjci3q6pD0gqp+vvBRYBezrYV97vbiOwUlK/pIWpr6c61Vfy28BPI2K4Wujk9sr7+0An32OdmHnvlQvZjP4/kCX+F7vcy2+Q7fY9D2xPl5uB7wAvpPpGYE6H+3ov2ZEizwE7q9sJuBh4DNidfl7UhW32LuA1YGZNrSvbiyyg9gMnyP7XdnveNiLb5b8vve9eAAY73NcQ2Xh09X32p2nZf5N+x88BzwAf73Bfub874Itpe70ELO9kX6n+beA/jlm2k9sr7+9Dx95jPkPazMzqFGlYyczMxsnhYGZmdRwOZmZWx+FgZmZ1HA5mZlbH4WBmZnUcDmZmVsfhYGZmdf4/bS2ZESgF+RsAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x10de8b0b8>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "\n",
    "x = np.random.random((1000,2))\n",
    "\n",
    "w = np.array([[2.4],[3.9]])\n",
    "b = 4.2\n",
    "\n",
    "y = np.dot(x, w) + b\n",
    "y = y + 0.1 * np.random.random(1000).reshape(1000,1)\n",
    "\n",
    "w_ = np.random.randn(2,1)\n",
    "b_ = 0\n",
    "\n",
    "\n",
    "lr = 0.0001\n",
    "epochs = 200\n",
    "batch = 10\n",
    "\n",
    "a = []\n",
    "for e in range(epochs):\n",
    "    for i in range(int(len(x)/batch)):\n",
    "        x_ = x[batch*i : batch*(i+1)]\n",
    "        y_ = y[batch*i : batch*(i+1)]\n",
    "        _w = 0\n",
    "        _b = 0\n",
    "        for j in range(len(x_)):\n",
    "            _w = _w + 2 * x_[j] * (np.dot(x_[j], w_) + b_ - y_[j])\n",
    "            _b = _b + 2 * (np.dot(x_[j], w_) + b_ - y_[j]).sum()\n",
    "        _w = _w.reshape(2,1)\n",
    "        w_ = w_ - lr * _w\n",
    "        b_ = b_ - lr * _b\n",
    "    a.append(((np.dot(x, w_) + b_ - y)**2).sum())\n",
    "        \n",
    "print(w_, b_, a[-1])\n",
    "plt.plot(np.arange(epochs),a)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
